package com.home.base.util;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebClientOptions;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.springframework.util.ObjectUtils;

import java.util.List;

public class WebClientUtils {
    static WebClientUtils instance;
    WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER);
    String baseUri="https://cbss.10010.com/essframe";
    public WebClientUtils(){
        WebClientOptions clientOptions = webClient.getOptions();
        // 设置webClient的相关参数
        clientOptions.setJavaScriptEnabled(true);
        clientOptions.setCssEnabled(false);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        clientOptions.setTimeout(35000);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        clientOptions.setThrowExceptionOnScriptError(false);
    }

    public static WebClientUtils getInstance(){
        if(ObjectUtils.isEmpty(instance)){
            instance=new WebClientUtils();
        }
        return instance;
    }

    public void open() throws Exception{
        HtmlPage htmlPage=webClient.getPage(baseUri);
       // List<HtmlElement> elements=htmlPage.getByXPath("//div[@class='w-head__loginbtn']");
        System.out.println("button:::"+htmlPage.asXml());
    }

    public static void main(String[] args){
        try {
            WebClientUtils clientUtils=  WebClientUtils.getInstance();
            clientUtils.open();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
